$().ready(function () {
    //开始直播
    var ws = new WebSocket("ws://47.105.60.14:2000");
    $("#danmu").danmu({
        left: 0,    //区域的起始位置x坐标
        top: 0,  //区域的起始位置y坐标
        height: 600, //区域的高度
        width: 1200, //区域的宽度
        zindex :100, //div的css样式zindex
        speed:20000, //弹幕速度，飞过区域的毫秒数
        sumtime:900 , //弹幕运行总时间
        danmuss:{}, //danmuss对象，运行时的弹幕内容
        default_font_color:"#FFFFFF", //弹幕默认字体颜色
        font_size_small:16, //小号弹幕的字体大小,注意此属性值只能是整数
        font_size_big:24, //大号弹幕的字体大小
        opacity:"0.9", //弹幕默认透明度
        top_botton_danmu_time:6000, //顶端底端弹幕持续时间,
    } );

    $('.start_live').click(function () {
        //变成不可选
        $(this).addClass("layui-btn-disabled");
        //变成可选
        $('.danmu_start').removeClass('layui-btn-disabled');
        //开始弹幕stop_live
        $('.stop_live').removeClass('layui-btn-disabled');
        $('#danmu').danmu('danmu_start');
        //获得用户的基本信息
        $.ajax({
            type: "get",
            url: '/index.php?m=Home&c=User&a=getselfDetail',
            dataType: "json",
            //data:{"id": $.getUrlParam('id')},
            success: function (data) {
                layer.msg("您的播放地址为" + data.data.live_address);
                //弹幕初始化
                var video_width = 1200;
                var video_height= 600;
                //初始化播放器
                player = cyberplayer("playercontainer").setup({
                    width :video_width,
                    height : video_height,
                    backcolor : "#FFFFFF",
                    stretching : "uniform",
                    file : "/LiveAddress/"+ data.data.live_address+"/index.m3u8",
                    ak : "9e2889da373d4eb188a82365b2f2c918",
                    autoStart : true,
                    repeat : false,
                    volume : 100,
                    controls : "over"
                });
                //初始化弹幕

            }
        })
        //发送用户信息后台分配信息
        param = {};
        param.access_token=tool.cookie('access_token');
        //param.id=GetQueryString('id');
        param.type = 'token'
        param.content = "";
        ws.onopen = function() {
            ws.send(JSON.stringify(param));

        };
        ws.onmessage = function(e) {
            ws_data = JSON.parse(e.data);
            switch (ws_data.type) {
                case "connect":
                    e;
                    break;
                case "live":
                    e;
                    break;
                case "close":
                    ws.close();
                    break;
                case "message":
                    var html = ' <div class="admin-group">' +
                        '            <img class="admin-img" src="/Public/img/girl.png"/>' +
                        '            <div class="admin-msg">' +
                        '                <i class="triangle-admin"></i>' +
                        '                <span class="admin-reply">' + ws_data.content + '</span>' +
                        '            </div>' +
                        '        </div>'
                    $('.mobile-page').append(html);
                    info = {
                        "text": ws_data.content,
                        "color": "red",
                        "size": "10",
                        "position": "0",
                        "time": $('#danmu').data("nowtime") + 5
                    };
                    $('#danmu').danmu("add_danmu", info);
                    break;
            }
        };
    })
    //停止直播
    $('.stop_live').click(function () {
        $(this).addClass("layui-btn-disabled");
        $('.start_live').removeClass('layui-btn-disabled');
        $.ajax({
            type: "post",
            url:'/index/ListLive/stopLive',
            dataType: "json",
            data: {
                'access_token':$.cookie('access_token')
            },
            success: function (data) {
                layer.msg(data.errmsg);
                player.remove();
            }
        })
        ws.close();
    })
    //发送弹幕
    $('.danmu_start').click(function () {
        info = {"text": $('.danmu').val(), "color": "red", "size": "10", "position": "0", "time": $('#danmu').data("nowtime")+5,"isnew":""};
        $('#danmu').danmu("add_danmu", info);
        param = {};
        param.id=GetQueryString('id');
        param.information=$('.danmu').val();
        param.time=$('#danmu').data("nowtime");
        param = JSON.stringify(param);
        ws.send(param);
        var html ='<div class="user-group">' +
            '            <div class="user-msg">' +
            '                <span class="user-reply">'+$('.danmu').val()+'</span>' +
            '                <i class="triangle-user"></i>' +
            '            </div>' +
            '            <img class="user-img" src="/Public/img/boy.png"/>' +
            '        </div>'
        $('.mobile-page').append(html);
    })

})
function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
